<?php


namespace app\model\partner;


use app\model\config\ConfigOrderSourceModel;
use app\model\config\RegionModel;
use app\model\user\UserModel;
use think\Model;
use think\model\concern\SoftDelete;

class EscortMenuOrderModel extends Model
{
    protected $name = 'escort_menu_order';
    protected $autoWriteTimestamp = true;

    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $json = ['city_id'];
    protected $jsonAssoc = true;

    protected $append = ['pay_status_txt', 'valid_time_txt', 'city_txt'];

    protected $statusTxtArr = [
        -1 => '支付失败',
        1 => '待支付',
        2 => '使用中',
        3 => '已完成',
        4 => '已取消'
    ];

    public function user()
    {
        return $this->hasOne(UserModel::class, 'id', 'user_id')->bind(['username', 'mobile']);
    }

    public function userCity()
    {
        return $this->hasOne(RegionModel::class, 'id', 'user_city_id')->bind(['region_name' => 'name']);
    }

    /**
     * 状态
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getPayStatusTxtAttr($value, $data)
    {
        if (!isset($data['pay_status'])) {
            return '';
        }
        return $this->statusTxtArr[$data['pay_status']];
    }

    public function getValidTimeTxtAttr($value, $data)
    {
        if (!isset($data['valid_time'])) {
            return '';
        }
        return date('Y-m-d H:i');
    }

    public function getCityTxtAttr($value, $data)
    {
        if (!isset($data['city_id'])) {
            return '';
        }
        $cityArr = RegionModel::whereIn('id', $data['city_id'])->column('name');
        return implode(',', $cityArr);
    }

    public function source()
    {
        return $this->hasOne(ConfigOrderSourceModel::class, 'id', 'source_id')->bind(['source_title' => 'title']);
    }
}